package robocup.testing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedList;

import robocup.component.actions.DashAction;
import robocup.component.actions.TurnAction;
import robocup.component.geometry.Circle;
import robocup.component.geometry.CircleSegment;
import robocup.component.geometry.LineSegment;
import robocup.component.geometry.StraightLine;
import robocup.component.geometry.Vektor;
import robocup.component.geometry.analysers.ReachableArea;
import robocup.component.util.Randoms;
import robocup.component.worldobjects.Player;

/**
 * @author Thomas Karbe
 * 
 */
public class TestReachableArea
    extends GraphicalTestFrame {

  Randoms rand= Randoms.getInstance();
  Player             p           = rand.getRandomPlayer();
  ReachableArea      ra          = new ReachableArea();

  final TurnAction   TURN180     = new TurnAction(180);
  final DashAction   DASH100     = new DashAction(100);
  LinkedList<Vektor> listIn      = new LinkedList<Vektor>();

  Circle             dummyCircle = Circle.getDummyInstance();
  CircleSegment dummyCircleSegment= CircleSegment.getDummyInstance();
  LineSegment dummyLineSegment= LineSegment.getDummyInstance();
  StraightLine dummyStraightLine= StraightLine.getDummyInstance();

  /**
   * 
   */
  public TestReachableArea() {

    super();
  }

  /**
   * @param d
   */
  public TestReachableArea(
      Dimension d) {

    super(d);
  }

  /**
   * @param center
   */
  public TestReachableArea(
      Vektor center) {

    super(center);
  }

  /**
   * @param d
   * @param center
   */
  public TestReachableArea(
      Dimension d,
      Vektor center) {

    super(d, center);
  }

  /**
   * @param scale
   */
  public TestReachableArea(
      double scale) {

    super(scale);
  }

  /**
   * @param d
   * @param scale
   */
  public TestReachableArea(
      Dimension d,
      double scale) {

    super(d, scale);
  }

  /**
   * @param center
   * @param scale
   */
  public TestReachableArea(
      Vektor center,
      double scale) {

    super(center, scale);
  }

  /**
   * @param d
   * @param center
   * @param scale
   */
  public TestReachableArea(
      Dimension d,
      Vektor center,
      double scale) {

    super(d, center, scale);
  }

  /**
   * (non-Javadoc)
   * 
   * @see robocup.testing.GraphicalTestFrame#nextSituation()
   */
  @Override
  protected void nextSituation() {

    p = rand.getRandomPlayer();
    ra = new ReachableArea();
    ra.setArea(0, p, p.getKickDistance());
//    while (ra.getTurn3() >= 165) {
//      p= getRandomPlayer();
//      ra.setArea(0, p, p.getKickDistance());
//    }

    setCenter(p.getPosition());
//    System.out.println("d0: "
//        + ra.getDash0()
//        + " d1: "
//        + ra.getDash1()
//        + " d2: "
//        + ra.getDash2()
//        + " d3: "
//        + ra.getDash3()
//        + " d4: "
//        + ra.getDash4());
//    System.out.println("t1: "
//        + ra.getTurn1()
//        + " t2: "
//        + ra.getTurn2()
//        + " t3: "
//        + ra.getTurn3());
//    System.out.println("reachdist: " + ra.getReachDist());
    listIn.clear();
    dummyCircle.setCenter(rand.getRandomVektor(-4+p.getPosition().x, 4+p.getPosition().x, -4+p.getPosition().y, 4+p.getPosition().y));
    dummyCircle.setRadius(rand.getRandomDouble(1, 5));
    dummyCircleSegment.copyCenter(rand.getRandomVektor(-4+p.getPosition().x, 4+p.getPosition().x, -4+p.getPosition().y, 4+p.getPosition().y));
    dummyCircleSegment.setRadius(rand.getRandomDouble(1, 5));
    dummyCircleSegment.setStartAngle(rand.getRandomDouble(-180, 180));
    dummyCircleSegment.setEndAngle(rand.getRandomDouble(-180, 180));
    dummyLineSegment.copyPoint1(rand.getRandomVektor(-4+p.getPosition().x, 4+p.getPosition().x, -4+p.getPosition().y, 4+p.getPosition().y));
    dummyLineSegment.copyPoint2(rand.getRandomVektor(-4+p.getPosition().x, 4+p.getPosition().x, -4+p.getPosition().y, 4+p.getPosition().y));
    dummyStraightLine.setStraightLine(rand.getRandomVektor(-4+p.getPosition().x, 4+p.getPosition().x, -4+p.getPosition().y, 4+p.getPosition().y), rand.getRandomDouble(-180, 180));
//    System.out.println(dummyCircle);
    ra.addIntersectionPointsToList(dummyCircle, true, listIn);
    ra.addIntersectionPointsToList(dummyCircleSegment, true, listIn);
    ra.addIntersectionPointsToList(dummyStraightLine, true, listIn);
    ra.addIntersectionPointsToList(dummyLineSegment, true, listIn);    
//    System.out.println(listIn);
  }

  /**
   * (non-Javadoc)
   * 
   * @see robocup.testing.GraphicalTestFrame#nextCycle()
   */
  @Override
  protected void nextCycle() {

    ra.setArea(ra.getCycles() + 1, p, p.getKickDistance());
//    System.out.println("d0: "
//        + ra.getDash0()
//        + " d1: "
//        + ra.getDash1()
//        + " d2: "
//        + ra.getDash2()
//        + " d3: "
//        + ra.getDash3()
//        + " d4: "
//        + ra.getDash4());
//    System.out.println("t1: "
//        + ra.getTurn1()
//        + " t2: "
//        + ra.getTurn2()
//        + " t3: "
//        + ra.getTurn3());
//    System.out.println("reachdist: " + ra.getReachDist());
    listIn.clear();
    ra.addIntersectionPointsToList(dummyCircle, true, listIn);
    ra.addIntersectionPointsToList(dummyCircleSegment, true, listIn);
    ra.addIntersectionPointsToList(dummyStraightLine, true, listIn);
    ra.addIntersectionPointsToList(dummyLineSegment, true, listIn);
  }

  /**
   * (non-Javadoc)
   * 
   * @see robocup.testing.GraphicalTestFrame#paintSituation(java.awt.Graphics)
   */
  @Override
  protected void paintSituation(Graphics g) {

    g.setColor(Color.YELLOW);
    draw(g, p);
    g.setColor(Color.RED);
    draw(g, dummyCircle);
    draw(g, dummyCircleSegment);
    draw(g, dummyLineSegment);
    draw(g, dummyStraightLine);
    g.setColor(Color.BLACK);
    draw(g, ra, true);
    g.setColor(Color.red);
    draw(g, listIn);
  }

  /**
   * @param args
   */
  public static void main(String[] args) {

    new TestReachableArea();
  }

}
